package com.hyperzsoft.utils;

import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

public class DateUtils {
	public enum Formats {
		FORMAT_TIMESTAMP ("yyyy-MM-dd HH:mm:ss"),
		FORMAT_DATESTAMP ("yyyy-MM-dd"),
		FORMAT_SLASHDATE ("MM/dd/yyyy");
		
		private Formats(String aFormat) {
			//can't call super ctor here
			fFormat = aFormat;
		}
		
		final String getFormat() {
			return fFormat;
		}
		
		private final String fFormat;
	}
	
	static List<String> months = Arrays.asList(new DateFormatSymbols().getMonths());
	static List<String> shortMonths = Arrays.asList(new DateFormatSymbols().getShortMonths());
	static List<String> weekdays = Arrays.asList(new DateFormatSymbols().getShortMonths());
	static List<String> shortWeekdays = Arrays.asList(new DateFormatSymbols().getShortWeekdays());
	
	public static String dateNow(Formats dateFormat) {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat.getFormat());
		return sdf.format(cal.getTime());
	}
	 
	public static long createDateValue(int year, int month, int day) {
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day, 24, 0, 0);
		return cal.getTimeInMillis();
	}
	
	public static String createDateValue(int year, int month, int day, Formats format) {
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day, 24, 0, 0);
		SimpleDateFormat sdf = new SimpleDateFormat(format.getFormat());
		return sdf.format(cal.getTime());
	}
	
	public static String timestampNow() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(Formats.FORMAT_TIMESTAMP.getFormat());
		return sdf.format(cal.getTime());
	}
	
	public static long now() {
		Calendar cal = Calendar.getInstance();
		return cal.getTimeInMillis();
	}
	
	public static int getCurrentYearValue() {
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.YEAR);
	}
		
	public static String getMonthName(int monthToGet) {
		return months.get(monthToGet);
	}
	
	public static String getMonthName(int monthToGet, boolean shortName) {
		return (shortName) ? shortMonths.get(monthToGet) : getMonthName(monthToGet);
	}
	
	public static String getMonthName(int monthToGet, boolean shortName, Locale locale) {
		months = Arrays.asList(new DateFormatSymbols(locale).getMonths());
		shortMonths = Arrays.asList(new DateFormatSymbols(locale).getShortMonths());
		
		return (shortName) ? getMonthName(monthToGet, shortName) : getMonthName(monthToGet);
	}
	
	public static int getNumericMonth(String monthName) {
		return months.indexOf(monthName);
	}
	
	public static int getNumericMonth(String monthName, boolean shortName) {
		return (shortName) ? shortMonths.indexOf(monthName) : getNumericMonth(monthName);
	}
	
	public static int getNumericMonth(String monthName, boolean shortName, Locale locale) {
		months = Arrays.asList(new DateFormatSymbols(locale).getMonths());
		shortMonths = Arrays.asList(new DateFormatSymbols(locale).getShortMonths());
		
		return (shortName) ? getNumericMonth(monthName, shortName) : getNumericMonth(monthName);
	}
	
	public static String getWeekdayName(int weekdayToGet) {
		return weekdays.get(weekdayToGet);
	}
	
	public static String getWeekdayName(int weekdayToGet, boolean shortName) {
		return (shortName) ? shortWeekdays.get(weekdayToGet) : getWeekdayName(weekdayToGet);
	}
	
	public static String getWeekdayName(int weekdayToGet, boolean shortName, Locale locale) {
		weekdays = Arrays.asList(new DateFormatSymbols(locale).getWeekdays());
		shortWeekdays = Arrays.asList(new DateFormatSymbols(locale).getShortWeekdays());
		
		return (shortName) ? getWeekdayName(weekdayToGet, shortName) : getWeekdayName(weekdayToGet);
	}
}